#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt5 import QtWidgets,QtCore,QtGui
from ui_mycurve import Ui_Form
from PyQt5.QtWidgets import QFileDialog
import os
import logging
from PyQt5.QtCore import QPoint, Qt
from PyQt5.QtGui import QBrush, QColor, QPainter, QPen, QPixmap
from PyQt5.QtWidgets import QLabel


class PaintWidget(QLabel):
    def __init__(self,  *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Fill the canvas with the initial color.

        self.last_x, self.last_y = None, None
        self._pen_color = QColor("#000000")
        self._pen_width = 4
        self.pen = QPen()
        self.brush = QBrush()
        self.pixmap = QPixmap()
        self.antialiased=True
    def paintEvent(self, event):
        rect = QtCore.QRect(10, 20, 80, 60)

        path = QtGui.QPainterPath()
        path.moveTo(20, 80)
        path.lineTo(20, 30)
        path.cubicTo(80, 0, 50, 50, 80, 80)

        startAngle = 30 * 16
        arcLength = 120 * 16

        painter = QPainter(self)
        painter.setPen(self.pen)
        painter.setBrush(self.brush)
        if self.antialiased:
            painter.setRenderHint(QPainter.Antialiasing)
        painter.drawLine(rect.bottomLeft(), rect.topRight())
        painter.setPen(self.palette().dark().color())
        painter.setBrush(Qt.NoBrush)
        painter.drawRect(QtCore.QRect(0, 0, self.width() - 1, self.height() - 1))

    def setPenColor(self, c):
        self._pen_color = QColor(c)

    def setPenWidth(self, w):
        self._pen_width = int(w)

    def mouseMoveEvent(self, e):
        if self.last_x is None:  # First event.
            self.last_x = e.x()
            self.last_y = e.y()
            return  #  Ignore the first time.

        # painter = QPainter(self.pixmap())
        # p = painter.pen()
        # p.setWidth(self._pen_width)
        # p.setColor(self._pen_color)
        # painter.setPen(p)
        # painter.drawLine(self.last_x, self.last_y, e.x(), e.y())
        # painter.end()
        # self.update()

        # # Update the origin for next time.
        # self.last_x = e.x()
        # self.last_y = e.y()

    def mousePressEvent(self, e):
        pass
        # if e.button() == Qt.MouseButton.RightButton:
        #     self._flood_fill_from_event(e)

    def mouseReleaseEvent(self, e):
        pass
        # self.last_x = None
        # self.last_y = None

    # def _flood_fill_from_event(self, e):
    #     image = self.pixmap().toImage()
    #     w, h = image.width(), image.height()
    #     x, y = e.x(), e.y()

    #     # Get our target color from origin.
    #     target_color = image.pixel(x, y)

    #     have_seen = set()
    #     queue = [(x, y)]

    #     def get_cardinal_points(have_seen, center_pos):
    #         points = []
    #         cx, cy = center_pos
    #         for x, y in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
    #             xx, yy = cx + x, cy + y
    #             if (
    #                 xx >= 0
    #                 and xx < w
    #                 and yy >= 0
    #                 and yy < h
    #                 and (xx, yy) not in have_seen
    #             ):
    #                 points.append((xx, yy))
    #                 have_seen.add((xx, yy))

    #         return points

    #     # Now perform the search and fill.
    #     painter = QPainter(self.pixmap())
    #     painter.setPen(QPen(self._pen_color))

    #     while queue:
    #         x, y = queue.pop()
    #         if image.pixel(x, y) == target_color:
    #             painter.drawPoint(QPoint(x, y))
    #             queue.extend(get_cardinal_points(have_seen, (x, y)))

    #     self.update()
class DlgSetting(QtWidgets.QDialog):
    def __init__(self, parent=None):
        logging.info("init")
        super(DlgSetting, self).__init__(parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.h = QtWidgets.QVBoxLayout(self.ui.widget)
        self.paint=PaintWidget()
        self.h.addWidget(self.paint)
        self.ui.pushButton_fit.clicked.connect(self.fit)
    def fit(self):

        pass
    def ok(self):
        self.accept()
    def cancel(self):
        self.reject()



def main():
    import sys
    app = QtWidgets.QApplication(sys.argv)
    calculator = DlgSetting()
    calculator.exec()


if __name__ == '__main__':
    main()
